package com.example.viberapp;

import java.util.ArrayList;
import java.util.List;

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.bonuspack.overlays.Marker;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.widget.Toast;

public class MapActivity extends Activity implements LocationListener{

	private MapView osm;
	private MapController mc;
	private LocationManager locationManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map);
		 
		// Sets the tile of map to the default(Mapnik), the zoom controls and touch controls
        osm=(MapView) findViewById(R.id.mapview);
        osm.setTileSource(TileSourceFactory.MAPNIK);
        osm.setBuiltInZoomControls(true);
        osm.setMultiTouchControls(true);
        
        // Gets the mapController
        
        mc = (MapController) osm.getController();
        mc.setZoom(12);
        
        // Gets the actual location of user
        
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,this);
        Location location2 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        GeoPoint center = new GeoPoint(location2.getLatitude(),location2.getLongitude());
        mc.animateTo(center);
        
        Marker marker = new Marker(osm);
    	marker.setPosition(center);
        //addMarker(center);
    	osm.getOverlays().add(marker);
    	//refreshes the map
    	osm.invalidate();
    	
        //Builds the points of surf(to be modified)
        List<GeoPoint> surfers = new ArrayList<GeoPoint>();
        GeoPoint surf2 = new GeoPoint(-3.70143,-38.56930);
    	surfers.add(surf2);
    	GeoPoint surf3 = new GeoPoint(-3.73866,-38.45117);
    	surfers.add(surf3);
    	
    	 for (int i = 0; i < surfers.size(); i++) {
 			addMarker(surfers.get(i));
 		}
    	 
    	 
	}
	
	public void addMarker(GeoPoint location){
    	Marker marker = new Marker(osm);
    	marker.setPosition(location);
    	//marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    	marker.setIcon(getResources().getDrawable(R.drawable.point));
    	
    	//osm.getOverlays().clear();
    	osm.getOverlays().add(marker);
    	//refreshes the map
    	osm.invalidate();
    }
	
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.map, menu);
		return true;
	}

	@Override
	public void onLocationChanged(Location arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub
		
	}

}
